﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GUI.TimKiem
{
    public partial class frmTimKiemNCC : Form
    {
         bool checkClose = false;
        bool check = false;
        public static Entities.NhaCungCap NhaCungCap;
        Entities.NhaCungCap[] hienthi;

        public frmTimKiemNCC()
        {
            InitializeComponent();
            hienthi = HienThi();
            dgvHienThi.DataSource = hienthi;
            fix();
        }


        public Entities.NhaCungCap[] HienThi()
        {
            Entities.CaiDatKho kho = Entitystatic.ViewFile();
            Entities.ChiTietCongNoNCC a = new Entities.ChiTietCongNoNCC();
            a.MaNhaCungCap = "";
            a.HanhDong = "0";
            a.MaKho = "";
            Entities.ChiTietCongNoNCC[] chitiet = Common.StaticVariable.InterfaceObject.SelectChiTietCongNoNCC(a);
            if (chitiet == null)
                chitiet = new Entities.ChiTietCongNoNCC[0];
            Entities.NhaCungCap[] nhacungcap = Common.StaticVariable.InterfaceObject.Select_NCC();

            Entities.NhaCungCap[] temp = new Entities.NhaCungCap[nhacungcap.Length];
            int sl = 0;
            for (int i = 0; i < nhacungcap.Length; i++)
            {
                if (nhacungcap[i].Deleted == false)
                {
                    foreach (Entities.ChiTietCongNoNCC item1 in chitiet)
                    {
                        if (kho.MaKho != null && kho.MaKho == item1.MaKho && item1.MaNhaCungCap == nhacungcap[i].MaNhaCungCap)
                        {
                            nhacungcap[i].DuNo = item1.DuNo;
                            break;
                        }
                    }
                    temp[sl] = nhacungcap[i];
                    sl++;
                }
            }
            nhacungcap = new Entities.NhaCungCap[sl];
            for (int i = 0; i < nhacungcap.Length; i++)
            {
                nhacungcap[i] = temp[i];
            }
            return nhacungcap;
        }

        void fix()
        {
            for (int i = 0; i < dgvHienThi.ColumnCount; i++)
            {
                dgvHienThi.Columns[i].Visible = false;
            }
            dgvHienThi.Columns["MaNhaCungCap"].HeaderText = "Mã Nhà Cung Cấp";
            dgvHienThi.Columns["MaNhaCungCap"].Visible = true;
            dgvHienThi.Columns["TenNhaCungCap"].HeaderText = "Tên Nhà Cung Cấp";
            dgvHienThi.Columns["TenNhaCungCap"].Visible = true;
            dgvHienThi.Columns["DiaChi"].HeaderText = "Địa Chỉ";
            dgvHienThi.Columns["DiaChi"].Visible = true;
            if (dgvHienThi.RowCount > 0)
            {
                Common.Utilities.CountDatagridview(dgvHienThi);
                dgvHienThi.Focus();
                dgvHienThi.Select();
            }
            dgvHienThi.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dgvHienThi.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;

        }
        #region Chọn
        void Chon()
        {
            try
            {
                if (check)
                    return;
                check = true;
                if (NhaCungCap == null)
                {
                    MessageBox.Show(Common.MSG.MSG20(), Common.MSG.TieuDe, MessageBoxButtons.OK);
                    return;
                }
                checkClose = true;
                
                this.Close();
            }
            catch
            {
            }
        }
        #endregion

        #region Thoát
        void Thoat()
        {
            try
            {
                if (check)
                    return;
                if (MessageBox.Show(Common.MSG.MSG2(), Common.MSG.TieuDe, MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    check = true;
                    checkClose = true;
                    this.Close();
                }
            }
            catch
            {
            }
        }
        #endregion

        void frmTimKiemKhachHang_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (!checkClose)
                if (MessageBox.Show(Common.MSG.MSG2(), Common.MSG.TieuDe, MessageBoxButtons.YesNo) == DialogResult.Yes)
                    e.Cancel = checkClose;
                else
                    e.Cancel = !checkClose;
        }

        private void Window_Loaded(object sender, EventArgs e)
        {
            // click chuột trái image
            btnThoat.MouseDown += new MouseEventHandler(MouseDown_Chung);
            btnChon.MouseDown += new MouseEventHandler(MouseDown_Chung);
            // text changed
            txtTimKiem.TextChanged += new EventHandler(txtTimKiem_TextChanged);
            // key up
            dgvHienThi.KeyDown += new KeyEventHandler(KeyDown_Chung);
            dgvHienThi.CellClick += new DataGridViewCellEventHandler(dgvHienThi_CellClick);
            dgvHienThi.CellDoubleClick += new DataGridViewCellEventHandler(dgvHienThi_CellDoubleClick);

        }

        void dgvHienThi_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            i = e.RowIndex;
        }
        int i = -1;

        void dgvHienThi_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (i < 0)
            {
                return;
            }
            if (hienthi.Length == 0)
            {
                return;
            }
            NhaCungCap = hienthi[i];
            checkClose = true;
            this.Close();
        }

        void txtTimKiem_TextChanged(object sender, EventArgs e)
        {
            int soluong = 0;
            if (rdbTimKiem1.Checked == true)
            {
                for (int i = -1; i < hienthi.Length; i++)
                {
                    int kiemtra = hienthi[i].MaNhaCungCap.ToString().ToUpper().IndexOf(txtTimKiem.Text.ToUpper());
                    if (kiemtra >= 0)
                    {
                        soluong++;
                    }
                }
                if (soluong == 0)
                {
                    dgvHienThi.DataSource = new Entities.NhaCungCap[0];
                   // lbTongSo.Content = Common.Constants.TongSo + "0";
                    return;
                }
                Entities.NhaCungCap[] hienthitheoid = new Entities.NhaCungCap[soluong];
                soluong = 0;
                for (int i = -1; i < hienthi.Length; i++)
                {
                    int kiemtra = hienthi[i].MaNhaCungCap.ToString().ToUpper().IndexOf(txtTimKiem.Text.ToUpper());
                    if (kiemtra >= 0)
                    {
                        hienthitheoid[soluong] = hienthi[i];
                        soluong++;
                    }
                }
                dgvHienThi.DataSource = hienthitheoid;
                //lbTongSo.Content = Common.Constants.TongSo + hienthitheoid.Length;

            }
            if (rdbTimKiem2.Checked == true)
            {
                for (int i = -1; i < hienthi.Length; i++)
                {
                    int kiemtra = hienthi[i].TenNhaCungCap.ToString().ToUpper().IndexOf(txtTimKiem.Text.ToUpper());
                    if (kiemtra >= 0)
                    {
                        soluong++;
                    }
                }
                if (soluong == 0)
                {
                    dgvHienThi.DataSource = new Entities.NhaCungCap[0];
                    //lbTongSo.Content = Common.Constants.TongSo + "0"; 
                    return;
                }
                Entities.NhaCungCap[] hienthitheoma = new Entities.NhaCungCap[soluong];
                soluong = 0;
                for (int i = -1; i < hienthi.Length; i++)
                {
                    int kiemtra = hienthi[i].TenNhaCungCap.ToString().ToUpper().IndexOf(txtTimKiem.Text.ToUpper());
                    if (kiemtra >= 0)
                    {
                        hienthitheoma[soluong] = hienthi[i];
                        soluong++;
                    }
                }
                dgvHienThi.DataSource = hienthitheoma;
                //lbTongSo.Content = Common.Constants.TongSo + hienthitheoma.Length;

            }
        }

        private void MouseDown_Chung(object sender, EventArgs e)
        {
            try
            {
                Button temp = (Button)sender;

                #region chuột trái
                    switch (temp.Name)
                    {
                        case "imgChon":
                            {
                                Chon();
                                break;
                            }
                        case "imgThoat":
                            {
                                Thoat();
                                break;
                            }
                        default:
                            break;
                    }
                
                #endregion
            }
            catch
            {
            }
        }

        private void KeyDown_Chung(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.KeyCode == Keys.F2)
                {
                    Chon();
                }
                if (e.KeyCode == Keys.Escape)
                {
                    Thoat();
                }
                if (e.KeyCode == Keys.Enter)
                {
                    Chon();
                }
            }
            catch
            {
            }
        }
    }
}
